<?php
namespace app\index\controller;

use Single;

class Index
{
    public function index()
    {
        /**单例模式测试代码 */
        print_r(\Single::getInstance()->abc());
        print_r(\Single::getInstance()->abc());
        print_r(\Single::getInstance()->abc());
        print_r(\Single::getInstance()->abc());
        print_r(\Single::getInstance()->abc());
        print_r(\Single::getInstance()->abc());
    }

    public function register(){
        $a=new \A();
        // \SingwaRegister::set('singwa',$a); //添加到注册树中

        // $abc=\SingwaRegister::get('singwa'); //获取到当前模型
        
        $abc=\SingwaRegister::get('A'); //获取到当前模型

        dump($abc->abc());

    }

    public function personbuy(){ //测试依赖注入

        // $bmw=new \di\Car();
        // $person=new \di\Person();
        // var_dump($person->buy($bmw));

        \di\Container::getInstance()->set('person','\di\Person');
        \di\Container::getInstance()->set('car','\di\Car');

        $obj=\di\Container::getInstance()->get('person');

        var_dump($obj->buy());
    }

    public function rel(){ //反射
        $obj=new \A();
        // var_dump($obj);

        $obj2=new \ReflectionClass($obj);

        $instance=$obj2->newInstance(); //相当于实例化A类

        // var_dump($instance);

        $methods=$obj2->getMethods(); //获取类中的方法
        // var_dump($methods);

        foreach ($methods as $key => $m) {
            var_dump($m->getDocComment()); //获取注释内容
        }

        $properties=$obj2->getProperties(); //获取类的属性
        // var_dump($properties);

        //调用方法 方式一
        echo $instance->abc(1,2);
        //调用方法 方式二
        $method=$obj2->getMethod("abc");
        echo $method->invokeArgs($instance,['mmmm','555444a']);
        //调用方法 方式三
        $method=$obj2->getMethod("ddd");
        echo $method->invoke($instance);

        /**判断某个方式是否是公用的 */
        $method=new \ReflectionMethod($obj,"ddd");
        if($method->isPublic()){
            echo '是公用方法';
        }

        $method2=new \ReflectionMethod($obj,"abc");
        dump($method2->getParameters()); //获取参数
        dump($method2->getNumberOfParameters()); //获取参数个数
    }

    public function count(){
        $obj=new \SingwaCount();
        // echo $obj->count();

        echo count($obj);
    }
}
